package br.unipar.academico.mail;


import java.security.Security;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SendMail {

    private String mailSmtpServer;
    private String mailSmtpServerPort;
    
    public SendMail() {
        mailSmtpServer = "smtp.gmail.com";
        mailSmtpServerPort = "465";
    }

    public SendMail(String server, String port) {
        this.mailSmtpServer = server;
        this.mailSmtpServerPort = port;
    }

    private String from = "unipar_cia@mhfelipe.com";
    private String passwd = "unipar";

    public void sendMail(String to, String subject, String message) {
        Properties props = new Properties();

        //props.setProperty("proxySet", "true");
        //props.setProperty("socksProxyHost", "cache.cia.unipar.br");
        //props.setProperty("socksProxyPort", "3128");

        Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());

		props.setProperty("mail.transport.protocol", "smtp");
		props.setProperty("mail.smtp.host", mailSmtpServer);
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.port", "465");
		props.put("mail.smtp.socketFactory.port", "465");
		props.put("mail.smtp.socketFactory.class",
		"javax.net.ssl.SSLSocketFactory");
		props.put("mail.smtp.socketFactory.fallback", "false");
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
		props.setProperty("mail.smtp.quitwait", "false");


        //props.put("mail.transport.protocol", "smtp");
        //props.put("mail.smtp.starttls.enable", "true");
        //props.put("mail.smtp.host", mailSmtpServer);
        //props.put("mail.smtp.auth", "true");
        //props.put("mail.smtp.user", from);
        //props.put("mail.smtp.debug", "true");
        //props.put("mail.smtp.port", mailSmtpServerPort);
        //props.put("mail.smtp.ssl", "true");
        //props.put("mail.smtp.socketFactory.port", mailSmtpServerPort);
        //props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        //props.put("mail.smtp.socketFactory.fallback", "false");

        SimpleAuth auth = new SimpleAuth(from, passwd);

        Session session = Session.getDefaultInstance(props, auth);
        session.setDebug(true);

        Message msg = new MimeMessage(session);

        try {
            msg.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
            msg.setFrom(new InternetAddress(from));
            msg.setSubject(subject);
            msg.setContent(message, "text/plain");
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            Transport tr = session.getTransport("smtp");
            tr.connect(mailSmtpServer, from, passwd);
            msg.saveChanges();
            tr.sendMessage(msg, msg.getAllRecipients());
            tr.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private class SimpleAuth extends Authenticator {

        private String user;
        private String pw;

        public SimpleAuth(String user, String pw) {
            this.user = user;
            this.pw = pw;
        }

        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(user, pw);
        }

    }

}
